package com.shinewind.home.controller;

import cn.hutool.core.util.BooleanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.shinewind.common.constant.messageConstant;
import com.shinewind.home.service.INewsService;
import com.shinewind.pojo.entity.News;
import com.shinewind.pojo.result.Result;
import com.shinewind.pojo.vo.NewsVo;
import com.shinewind.pojo.vo.PageVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/index")
@Api(value = "新闻接口", description = "用于实现新闻相关操作")
public class NewsController {
    @Autowired
    private INewsService iNewsService;

    @GetMapping("/model/news/list")
    @ApiOperation(value = "查询新闻列表", notes = "查询", response = Result.class)
    public PageVo<NewsVo> getNewsInfo(){
        List<News> newsList = iNewsService.getNewsInfo();
        List<NewsVo> newsVoList = newsList.stream().map(news -> {
            NewsVo newsVo = NewsVo.builder().build();
            BeanUtils.copyProperties(news,newsVo);
            return newsVo;
        }).collect(Collectors.toList());
        return new PageVo<NewsVo>(newsVoList,BooleanUtil.isTrue(newsVoList.size()>0),
                messageConstant.QUERY_SUCCESS,newsVoList.size());
    }

    @GetMapping("/model/news/{mId}")
    @ApiOperation(value = "查询单个新闻信息", notes = "查询", response = Result.class)
    public Result getNewsInfoById(@PathVariable("mId") String mId){
        News news = iNewsService.getNewsInfoById(mId);
        NewsVo newsVo = NewsVo.builder().build();
        BeanUtils.copyProperties(news,newsVo);
        return Result.success(newsVo, ObjectUtil.isNotNull(newsVo),
                messageConstant.QUERY_SUCCESS);
    }
}